package com.firebird.app.fekpsl.adapter.exam

import android.text.Html
import android.text.Html.FROM_HTML_MODE_LEGACY
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.bean.BlankFillingQuestion
import com.firebird.app.fekpsl.bean.QuestionTestContentBean
import com.google.gson.Gson

/**
 * Created by firebird on 2018/4/22.
 */
class BlankFillingQuestionAdapter : BaseQuickAdapter<BlankFillingQuestion, BaseViewHolder>(R.layout.exam_item_blank_filling_question) {
    inline fun <reified T> Gson.fromJson(json: String) = fromJson(json, T::class.java)
    override fun convert(helper: BaseViewHolder?, item: BlankFillingQuestion?) {
        helper ?: return
        item ?: return
        val index=helper.layoutPosition
        /*
        {"titleContent":"<span style=\"font-family: &quot;&quot;; font-size: 14px; background-color: #FFFFFF;\">
        This novel was once the&nbsp;<span class=\"gapfilling-span 9e940267-6eeb-495c-ad16-c912623142b8\">1</span>
        book in high schools in the United States. (read)</span>","analyze":"<span style=\"font-family: &quot;&quot;; font-size: 14px;
        background-color: #FFFFFF;\">这部小说曾经是美国高中</span><span style=\"text-decoration:underline;\">阅读最广泛的</span>
        <span style=\"font-family: &quot;&quot;; font-size: 14px; background-color: #FFFFFF;\">书。</span>",
        "questionItemObjects":[{"prefix":"1","content":"most widely read","score":50}],"correct":""}
         */
        helper.getView<TextView>(R.id.tv_blank_filling_answer_1).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_blank_filling_answer_2).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_blank_filling_answer_3).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_blank_filling_answer_4).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_blank_filling_answer_5).visibility = View.GONE
        helper.getView<EditText>(R.id.et_blank_filling_answer_1).visibility = View.GONE
        helper.getView<EditText>(R.id.et_blank_filling_answer_2).visibility = View.GONE
        helper.getView<EditText>(R.id.et_blank_filling_answer_3).visibility = View.GONE
        helper.getView<EditText>(R.id.et_blank_filling_answer_4).visibility = View.GONE
        helper.getView<EditText>(R.id.et_blank_filling_answer_5).visibility = View.GONE
        val QuestionTestContent=item.content
        val bean = Gson().fromJson<QuestionTestContentBean>(QuestionTestContent)
        val titleContent=bean.titleContent
        helper.setText(R.id.tv_blank_filling_question_title, Html.fromHtml(titleContent,FROM_HTML_MODE_LEGACY))
        bean.questionItemObjects.forEachIndexed{ index, s ->
            val answer_blank_filling=bean.questionItemObjects.get(index).prefix + "." + bean.questionItemObjects.get(index).content
            when(index) {
                0-> {
                    //helper.setText(R.id.et_blank_filling_answer_1, answer_blank_filling)
                    helper.getView<TextView>(R.id.tv_blank_filling_answer_1).visibility = View.VISIBLE
                    helper.getView<EditText>(R.id.et_blank_filling_answer_1).visibility = View.VISIBLE
                }
                1-> {
                    //helper.setText(R.id.et_blank_filling_answer_2, answer_blank_filling)
                    helper.getView<TextView>(R.id.tv_blank_filling_answer_2).visibility = View.VISIBLE
                    helper.getView<EditText>(R.id.et_blank_filling_answer_2).visibility = View.VISIBLE
                }
                2-> {
                    //helper.setText(R.id.et_blank_filling_answer_3, answer_blank_filling)
                    helper.getView<TextView>(R.id.tv_blank_filling_answer_3).visibility = View.VISIBLE
                    helper.getView<EditText>(R.id.et_blank_filling_answer_3).visibility = View.VISIBLE
                }
                3-> {
                    //helper.setText(R.id.et_blank_filling_answer_4, answer_blank_filling)
                    helper.getView<TextView>(R.id.tv_blank_filling_answer_4).visibility = View.VISIBLE
                    helper.getView<EditText>(R.id.et_blank_filling_answer_4).visibility = View.VISIBLE
                }
                4-> {
                    //helper.setText(R.id.et_blank_filling_answer_5, answer_blank_filling)
                    helper.getView<TextView>(R.id.tv_blank_filling_answer_5).visibility = View.VISIBLE
                    helper.getView<EditText>(R.id.et_blank_filling_answer_5).visibility = View.VISIBLE
                }
            }
        }
        helper.setText(R.id.tv_blank_filling_question_no,(index+1).toString())
    }
}
